package restfuljobs

import org.springframework.dao.DataIntegrityViolationException
import groovyx.net.http.*
import static groovyx.net.http.ContentType.*
import groovyx.net.http.Method

class UserController extends AbstractRestController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
    
	UserController() {
		path = 'user'
	}
	
    def index() {
        redirect(action: "list", params: params)
    }
    
    def login() {
        def userInstance
        
        httpBuilder.request(rootPath, Method.GET, XML ) {
			uri.path = path + "/$params.id"			
			
			response.success = { resp, xml ->			  
			  xml.each {			  											
				userInstance = new UserWrapper()
				userInstance.id = it.attributes()['id'].toLong()				
				userInstance.setFromXML(it)					
			  }
			  
			}
			
			response.failure = { resp ->
				flash.message = message(code: 'default.not.found.message', args: [message(code: path+".label", default: path.capitalize()), params.id])
				redirect(action: "list")
				return
			}
		}

        println "User login $userInstance"
        session.user = userInstance
    	redirect(uri: '/')
    	return
    }
    
    def logout() {
    	session.user = null
    	redirect(uri: '/')
    }

    def list() {
        
        def userInstanceList = []
        
        httpBuilder.get( path: path,
			headers : [Accept : 'application/xml']) { resp, xml ->
			
			xml.user.each {
				def user = new UserWrapper()
				user.id = it.attributes()['id'].toLong()				
				user.setFromXML(it)					
				userInstanceList.add(user)
			}
		}
		
		[userInstanceList: userInstanceList, userInstanceTotal: userInstanceList.size()]
		
    }

    def create() {
        [userInstance: new User(params)]
    }

    def show() {
        def userInstance
        
        httpBuilder.request(rootPath, Method.GET, XML ) {
			uri.path = path + "/$params.id"			
			
			response.success = { resp, xml ->			  
			  xml.each {			  											
				userInstance = new UserWrapper()
				userInstance.id = it.attributes()['id'].toLong()				
				userInstance.setFromXML(it)					
			  }
			  return [userInstance: userInstance]
			}
			
			response.failure = { resp ->
				flash.message = message(code: 'default.not.found.message', args: [message(code: path+".label", default: path.capitalize()), params.id])
				redirect(action: "list")
				return
			}
		}		       
    }

    def edit() {
        def userInstance
        
        httpBuilder.request(rootPath, Method.GET, XML ) {
			uri.path = path + "/$params.id"			
			
			response.success = { resp, xml ->			  
			  xml.each {			  											
				userInstance = new UserWrapper()
				userInstance.id = it.attributes()['id'].toLong()				
				userInstance.setFromXML(it)					
			  }
			  return [userInstance: userInstance]
			}
			
			response.failure = { resp ->
				flash.message = message(code: 'default.not.found.message', args: [message(code: path+".label", default: path.capitalize()), params.id])
				redirect(action: "list")
				return
			}
		}
    }
    
}
